/**
 * @author helio
 */
if(!SALE.dao) SALE.dao = {}
if(!SALE.dao.local) SALE.dao.local = {}
if(!SALE.dao.local.PurchaseOrderDao) SALE.dao.local.PurchaseOrderDao = function(){
	var orderDoc;
}

SALE.dao.local.PurchaseOrderDao.prototype = {	

		/**
		 * 
		 */
		initOrderDoc : function() {
			if(!this.orderDoc) {
				this.orderDoc = LocalXMLFileUtil.getOrderDoc();
			}
			
			return this.orderDoc;
		},
		
		saveOrderDoc : function() {
			LocalXMLFileUtil.saveXML(this.orderDoc,LocalXMLFileUtil.getOrderFile());
		},
		
		insertOrderInfo : function(orderJson) {
			var root = this.orderDoc.documentElement;
			var maxid = root.getAttribute("maxid");
			if(!maxid) {
				maxid = 0;
			}
			var id = parseInt(maxid) + 1;
			orderJson["id"] = id;
			root.setAttribute("maxid",id);
			var orderNode = this.orderDoc.createElement("order");
			
			this.updateOrder(orderJson, orderNode);			
			root.appendChild(orderNode);
			
			this.saveOrderDoc();
		},
		
		modifyOrderInfo : function(orderJson) {
			var orderNode = this.searchOrderByID(orderJson.id);
			
			this.updateOrder(orderJson, orderNode);
			
			this.saveOrderDoc();
		},
		
		deleteOrderInfo : function(id) {
			var orderNode = this.searchOrderByID(id);
			var root = this.orderDoc.documentElement;
			if(orderNode != null) {
				root.removeChild(orderNode);
			}			
		},
		
		updateOrder : function(orderJosn, orderNode) {
			for (var attri in orderJosn) {
			
				if (attri.indexOf("old") != 0 && orderJosn[attri] && orderJosn[attri] != "") {
					var attriNodes = Ext.DomQuery.select("/" + attri, orderNode);
					if (attriNodes != null && attriNodes.length > 0) {
						orderNode.removeChild(attriNodes[0]);
					}
					
					var attriNode = this.orderDoc.createElement(attri);
					orderNode.appendChild(attriNode);
					
					if (attri == "details") {
						var details = orderJosn.details;						
						for (var i = 0; i < details.length; i++) {
							var detail = details[i];
							var detailNode = this.orderDoc.createElement("detail");
							attriNode.appendChild(detailNode);
							
							for (var attriDetail in detail) {
							
								var detailAttriNode = this.orderDoc.createElement(attriDetail);
								contextNode = this.orderDoc.createCDATASection(detail[attriDetail]);
								detailAttriNode.appendChild(contextNode);
								detailNode.appendChild(detailAttriNode);
							}
						}
					} else {						
						var contextNode = this.orderDoc.createCDATASection(orderJosn[attri]);
						attriNode.appendChild(contextNode);
					}
				}
				
			}	
		},
		
		searchOrderByID : function(id) {
			var root = this.orderDoc.documentElement;
			var objArray = Ext.query("order:has(id:nodeValue(" + id + "))", root);
			var result;
			
			if(objArray.length == 1) {
				result = objArray[0];
			}
			
			return result;
		},
		
		searchOrderByCode : function(code) {
			var root = this.orderDoc.documentElement;
			var objArray = Ext.query("order:has(code:nodeValue(" + code + "))", root);
			var result;
			
			if(objArray.length == 1) {
				result = objArray[0];
			}
			
			return result;
		},
		
		/**
		 * 修改入库接收的数量。
		 * @param {Object} receiveJson
		 */	
		modifyOrderProductReceiveNumber : function(receiveJson) {
			var order = this.searchPurchaseOrderJsonByCode(receiveJson.bcode);
				
			if(order) {
				var details = order.details;
				var endFlag = true;
				for(var i = 0; i < details.length; i++) {
					var detail = details[i];
					if(receiveJson.index) {
						if(detail.index == receiveJson.index) {
							var pnumber = detail.pnumber != null && detail.pnumber != "" ? parseFloat(detail.pnumber) : 0;;
							var rnumber = detail.receive_number != null && detail.receive_number != "" ? parseFloat(detail.receive_number) : 0;
							var number = receiveJson.number;
							rnumber = parseFloat(rnumber) + parseFloat(number);
							detail.receive_number = rnumber;
						}
					} else if(detail.pcode == receiveJson.pcode) {
						var pnumber = detail.pnumber != null && detail.pnumber != "" ? parseFloat(detail.pnumber) : 0;;
						var rnumber = detail.receive_number != null && detail.receive_number != "" ? parseFloat(detail.receive_number) : 0;
						var number = receiveJson.number;
						rnumber = parseFloat(rnumber) + parseFloat(number);
						detail.receive_number = rnumber;
					}
					
					if(detail.receive_number != detail.pnumber) {
						endFlag = false;
					}
				}
				
				if(endFlag) {
					order.end_flag = 1;
				}
				
				this.modifyOrderInfo(order);
			}
		},
		
		/**
		 * 根据编号，查询出采购单的所有信息，并封闭成JSON对象。
		 * @param {Object} code
		 */
		searchPurchaseOrderJsonByCode : function(code) {
			var order = this.searchOrderByCode(code);
			
			if (order) {
				var children = order.childNodes;
				var orderJson = {};
				
				for (var i = 0; i < children.length; i++) {
					var child = children[i];
					
					if (child.nodeType == 1) {
						if (child.nodeName == "details") {
							
							if (!orderJson.details) {
								orderJson.details = [];
							}
							
							var detailArray = Ext.query("/detail", child);
							
							Ext.each(detailArray, function(detail) {
								var attriArray = detail.childNodes;
								var detailJson = {}
								for (var j = 0; j < attriArray.length; j++) {
									var attri = attriArray[j];
									if (attri.nodeType == 1) {
										if (!detailJson[attri.nodeName] && attri.childNodes[0] != null) {
											detailJson[attri.nodeName] = attri.childNodes[0].nodeValue;
										}
									}
								}
								
								orderJson.details.push(detailJson);
													
							});
						} else {
							
							if (!orderJson[child.nodeName] && child.childNodes[0] != null) {
								orderJson[child.nodeName] = child.childNodes[0].nodeValue;
							}
						}
					}
				}	
				
				return orderJson;
			}
		},
		
		/**
		 * 根据ID，查询出采购单的所有信息，并封闭成JSON对象。
		 * @param {Object} code
		 */
		searchPurchaseOrderJson : function(id) {
			var order = this.searchOrderByID(id);
			
			if (order) {
				var children = order.childNodes;
				var orderJson = {};
				
				for (var i = 0; i < children.length; i++) {
					var child = children[i];
					
					if (child.nodeType == 1) {
						if (child.nodeName == "details") {
							
							if (!orderJson.details) {
								orderJson.details = [];
							}
							
							var detailArray = Ext.query("/detail", child);
							
							Ext.each(detailArray, function(detail) {
								var attriArray = detail.childNodes;
								var detailJson = {}
								for (var j = 0; j < attriArray.length; j++) {
									var attri = attriArray[j];
									if (attri.nodeType == 1) {
										if (!detailJson[attri.nodeName] && attri.childNodes[0] != null) {
											detailJson[attri.nodeName] = attri.childNodes[0].nodeValue;
										}
									}
								}
								
								orderJson.details.push(detailJson);
													
							});
						} else {
							
							if (!orderJson[child.nodeName] && child.childNodes[0] != null) {
								orderJson[child.nodeName] = child.childNodes[0].nodeValue;
							}
						}
					}
				}	
				
				return orderJson;
			}
		},
		
		/**
		 * 根据输入条件，查询出采购单基本信息。并封闭成数组。
		 * @param {Object} condition
		 */	
		searchPurchaseOrdersArray : function(condition) {
			var root = this.orderDoc.documentElement;
			var objArray;
			
			if(!condition.status || condition.status == "2") {
				objArray = Ext.query("order", root);
			} else {
				if(condition.status == "1") {
					objArray = Ext.query("order:has(status:nodeValue(true))", root);
				} else {
					objArray = Ext.query("order:not(order:has(status:nodeValue(true)))", root);
				}				
			}
			
			var result = [];
		    Ext.each(objArray, function(order) {
				var filterFlag = false;
				for (var attri in condition) {
					if(attri != "status") {
						var tempAttri = attri.replace("_to", "");
						tempAttri = tempAttri.replace("_from", "");
						var node = Ext.DomQuery.select("/"+ tempAttri +"/*", order)[0];
						if(condition[attri] != null && condition[attri] != "") {
							if(!node || node.nodeValue == null || node.nodeValue == "") {
								filterFlag = true;
								break;
							}
							
							if(attri.indexOf("date") > -1 && attri.indexOf("from") > -1) {
								if(Date.parse(node.nodeValue) < Date.parse(condition[attri].replaceAll("-", "/"))) {
									filterFlag = true;
									break;
								}
							} else if(attri.indexOf("_from") > -1) {
								if(node.nodeValue < condition[attri]) {
									filterFlag = true;
									break;
								}
							} else if(attri.indexOf("date") > -1 && attri.indexOf("_to") > -1) {
								if(Date.parse(node.nodeValue) > Date.parse(condition[attri].replaceAll("-", "/"))) {
									filterFlag = true;
									break;
								}
							} else if(attri.indexOf("_to") > -1) {
								if(node.nodeValue > condition[attri]) {
									filterFlag = true;
									break;
								}
							} else {
								if(node.nodeValue.toUpperCase().indexOf(condition[attri].toUpperCase()) < 0) {
									filterFlag = true;
									break;
								}
							}
						}
					}
				}
				
				if(!filterFlag) {
					var children = order.childNodes;
					var nodeJson = {};
					for(var i = 0; i < children.length; i++) {
						var child = children[i];
						
						if(child.nodeType == 1) {
							if (!nodeJson[child.nodeName] && child.childNodes[0] != null) {
								nodeJson[child.nodeName] = child.childNodes[0].nodeValue;
							}
						}			
					}
					
					result.push(nodeJson);
				}

		    });
			return result;
		},
		
		/**
		 * 根据输入条件，查询出采购单的所有信息，并封闭成数组。
		 * @param {Object} condition
		 */
		searchPurchaseOrderDetailsArray : function(condition) {
			var root = this.orderDoc.documentElement;
			var objArray;
			
			if(!condition.status || condition.status == "2") {
				objArray = Ext.query("order", root);
			} else {
				if(condition.status == "1") {
					objArray = Ext.query("order:has(status:nodeValue(true))", root);
				} else {
					objArray = Ext.query("order:not(order:has(status:nodeValue(true)))", root);
				}				
			}
			
			var result = [];
		    Ext.each(objArray, function(order) {
				var filterFlag = false;
				for (var attri in condition) {
					if(attri != "status") {
						var tempAttri = attri.replace("_to", "");
						tempAttri = tempAttri.replace("_from", "");
						var node = Ext.DomQuery.select("/"+ tempAttri +"/*", order)[0];
						if(condition[attri] != null && condition[attri] != "") {
							if(!node || node.nodeValue == null || node.nodeValue == "") {
								filterFlag = true;
								break;
							}
							
							if(attri.indexOf("date") > -1 && attri.indexOf("from") > -1) {
								if(Date.parse(node.nodeValue) < Date.parse(condition[attri].replaceAll("-", "/"))) {
									filterFlag = true;
									break;
								}
							} else if(attri.indexOf("_from") > -1) {
								if(node.nodeValue < condition[attri]) {
									filterFlag = true;
									break;
								}
							} else if(attri.indexOf("date") > -1 && attri.indexOf("_to") > -1) {
								if(Date.parse(node.nodeValue) > Date.parse(condition[attri].replaceAll("-", "/"))) {
									filterFlag = true;
									break;
								}
							} else if(attri.indexOf("_to") > -1) {
								if(node.nodeValue > condition[attri]) {
									filterFlag = true;
									break;
								}
							} else {
								if(node.nodeValue.toUpperCase().indexOf(condition[attri].toUpperCase()) < 0) {
									filterFlag = true;
									break;
								}
							}
						}
					}
				}
				
				if(!filterFlag) {
					
					var children = order.childNodes;
					var nodeJson = {};
					for(var i = 0; i < children.length; i++) {
						var child = children[i];
						
						if (child.nodeName == "details") {
							
							if (!nodeJson.details) {
								nodeJson.details = [];
							}
							
							var detailArray = Ext.query("/detail", child);
							
							Ext.each(detailArray, function(detail) {
								var attriArray = detail.childNodes;
								var detailJson = {}
								for (var j = 0; j < attriArray.length; j++) {
									var attri = attriArray[j];
									if (attri.nodeType == 1) {
										if (!detailJson[attri.nodeName] && attri.childNodes[0] != null) {
											detailJson[attri.nodeName] = attri.childNodes[0].nodeValue;
										}
									}
								}
								
								nodeJson.details.push(detailJson);
													
							});
						} else {
							
							if (!nodeJson[child.nodeName] && child.childNodes[0] != null) {
								nodeJson[child.nodeName] = child.childNodes[0].nodeValue;
							}
						}		
					}
					
					result.push(nodeJson);
				}

		    });
			return result;
		}
}
